
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using Apps.Models;
using Apps.Common;
using Microsoft.Practices.Unity;
using System.Transactions;
using Apps.BLL.Core;
using Apps.Locale;
using LinqToExcel;
using System.IO;
using System.Text;
using System.Linq.Expressions;
using Apps.IDAL.RC;
using Apps.Models.RC;
using Apps.IBLL.RC;
namespace Apps.BLL.RC
{
	public partial class RC_WEIGHING_TMPBLL: Virtual_RC_WEIGHING_TMPBLL,IRC_WEIGHING_TMPBLL
	{
        

	}
	public class Virtual_RC_WEIGHING_TMPBLL
	{
        [Dependency]
        public IRC_WEIGHING_TMPRepository m_Rep { get; set; }

		public virtual List<RC_WEIGHING_TMPModel> GetList(ref GridPager pager, string queryStr)
        {

            IQueryable<RC_WEIGHING_TMP> queryData = null;
            if (!string.IsNullOrWhiteSpace(queryStr))
            {
                queryData = m_Rep.GetList(
								a=>a.WEIGHING_TMP_ID.Contains(queryStr)
								|| a.PROCESS_ID.Contains(queryStr)
								
								
								|| a.NODE_ID.Contains(queryStr)
								|| a.WEIGHING_USER_ID.Contains(queryStr)
								
								
								
								
								);
            }
            else
            {
                queryData = m_Rep.GetList();
            }
            pager.totalRows = queryData.Count();
            //排序
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }
		 
		public virtual List<RC_WEIGHING_TMPModel> GetPagerList(ref GridPager pager, Expression<Func<RC_WEIGHING_TMP, bool>> whereLambda )
        {

            IQueryable<RC_WEIGHING_TMP> queryData = m_Rep.GetList(whereLambda); 
             
            pager.totalRows = queryData.Count();
            //排序
            queryData = LinqHelper.SortingAndPaging(queryData, pager.sort, pager.order, pager.page, pager.rows);
            return CreateModelList(ref queryData);
        }

		public virtual List<RC_WEIGHING_TMPModel> GetList(Expression<Func<RC_WEIGHING_TMP, bool>> whereLambda )
        {

            IQueryable<RC_WEIGHING_TMP> queryData = m_Rep.GetList(whereLambda); 
       
            return CreateModelList(ref queryData);
        }

		public virtual List<RC_WEIGHING_TMPModel> GetListByParentId(ref GridPager pager, string queryStr,object parentId)
        {
			return new List<RC_WEIGHING_TMPModel>();
		}

        public virtual List<RC_WEIGHING_TMPModel> CreateModelList(ref IQueryable<RC_WEIGHING_TMP> queryData)
        {

            List<RC_WEIGHING_TMPModel> modelList = (from r in queryData
                                              select new RC_WEIGHING_TMPModel
                                              {
													WEIGHING_TMP_ID = r.WEIGHING_TMP_ID,
													PROCESS_ID = r.PROCESS_ID,
													WEIGHING_TYPE = r.WEIGHING_TYPE,
													WEIGHT = r.WEIGHT,
													NODE_ID = r.NODE_ID,
													WEIGHING_USER_ID = r.WEIGHING_USER_ID,
													WEIGHING_TIME = r.WEIGHING_TIME,
													WEIGHING_IMAGE1 = r.WEIGHING_IMAGE1,
													WEIGHING_IMAGE2 = r.WEIGHING_IMAGE2,
													WEIGHING_IMAGE3 = r.WEIGHING_IMAGE3,
          
                                              }).ToList();

            return modelList;
        }

        public virtual bool Create(ref ValidationErrors errors, RC_WEIGHING_TMPModel model)
        {
            try
            {
                RC_WEIGHING_TMP entity = m_Rep.GetById(model.WEIGHING_TMP_ID);
                if (entity != null)
                {
                    errors.Add(Resource.PrimaryRepeat);
                    return false;
                }
                entity = new RC_WEIGHING_TMP();
               				entity.WEIGHING_TMP_ID = model.WEIGHING_TMP_ID;
				entity.PROCESS_ID = model.PROCESS_ID;
				entity.WEIGHING_TYPE = model.WEIGHING_TYPE;
				entity.WEIGHT = model.WEIGHT;
				entity.NODE_ID = model.NODE_ID;
				entity.WEIGHING_USER_ID = model.WEIGHING_USER_ID;
				entity.WEIGHING_TIME = model.WEIGHING_TIME;
				entity.WEIGHING_IMAGE1 = model.WEIGHING_IMAGE1;
				entity.WEIGHING_IMAGE2 = model.WEIGHING_IMAGE2;
				entity.WEIGHING_IMAGE3 = model.WEIGHING_IMAGE3;
  

                if (m_Rep.Create(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.InsertFail);
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

		
		 public virtual bool Deletes(ref ValidationErrors errors,Expression<Func<RC_WEIGHING_TMP, bool>> whereLambda)
         {
		        try
				{
					using (DBContainer db = new DBContainer())
					{
				
							foreach (var entity in db.RC_WEIGHING_TMP.Where(whereLambda))
							{
								db.Entry(entity).State = System.Data.Entity.EntityState.Deleted;
							}

							if(db.SaveChanges()>0)
							{
								return true;
							}
							else
							{
								return false;
							}
			
					}
				}
				catch (Exception ex)
				{
					errors.Add(ex.Message);
					ExceptionHander.WriteException(ex);
					return false;
				}
        }

         public virtual bool Delete(ref ValidationErrors errors, object id)
        {
            try
            {
                if (m_Rep.Delete(id) == 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

        public virtual bool Delete(ref ValidationErrors errors, object[] deleteCollection)
        {
            try
            {
                if (deleteCollection != null)
                {
                    using (TransactionScope transactionScope = new TransactionScope())
                    {
                        if (m_Rep.Delete(deleteCollection) == deleteCollection.Length)
                        {
                            transactionScope.Complete();
                            return true;
                        }
                        else
                        {
                            Transaction.Current.Rollback();
                            return false;
                        }
                    }
                }
                return false;
            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

		
       

        public virtual bool Edit(ref ValidationErrors errors, RC_WEIGHING_TMPModel model)
        {
            try
            {
                RC_WEIGHING_TMP entity = m_Rep.GetById(model.WEIGHING_TMP_ID);
                if (entity == null)
                {
                    errors.Add(Resource.Disable);
                    return false;
                }
                              				entity.WEIGHING_TMP_ID = model.WEIGHING_TMP_ID;
				entity.PROCESS_ID = model.PROCESS_ID;
				entity.WEIGHING_TYPE = model.WEIGHING_TYPE;
				entity.WEIGHT = model.WEIGHT;
				entity.NODE_ID = model.NODE_ID;
				entity.WEIGHING_USER_ID = model.WEIGHING_USER_ID;
				entity.WEIGHING_TIME = model.WEIGHING_TIME;
				entity.WEIGHING_IMAGE1 = model.WEIGHING_IMAGE1;
				entity.WEIGHING_IMAGE2 = model.WEIGHING_IMAGE2;
				entity.WEIGHING_IMAGE3 = model.WEIGHING_IMAGE3;
 


                if (m_Rep.Edit(entity))
                {
                    return true;
                }
                else
                {
                    errors.Add(Resource.NoDataChange);
                    return false;
                }

            }
            catch (Exception ex)
            {
                errors.Add(ex.Message);
                ExceptionHander.WriteException(ex);
                return false;
            }
        }

      

        public virtual RC_WEIGHING_TMPModel GetById(object id)
        {
            if (IsExists(id))
            {
                RC_WEIGHING_TMP entity = m_Rep.GetById(id);
                RC_WEIGHING_TMPModel model = new RC_WEIGHING_TMPModel();
                              				model.WEIGHING_TMP_ID = entity.WEIGHING_TMP_ID;
				model.PROCESS_ID = entity.PROCESS_ID;
				model.WEIGHING_TYPE = entity.WEIGHING_TYPE;
				model.WEIGHT = entity.WEIGHT;
				model.NODE_ID = entity.NODE_ID;
				model.WEIGHING_USER_ID = entity.WEIGHING_USER_ID;
				model.WEIGHING_TIME = entity.WEIGHING_TIME;
				model.WEIGHING_IMAGE1 = entity.WEIGHING_IMAGE1;
				model.WEIGHING_IMAGE2 = entity.WEIGHING_IMAGE2;
				model.WEIGHING_IMAGE3 = entity.WEIGHING_IMAGE3;
 
                return model;
            }
            else
            {
                return null;
            }
        }

		public virtual RC_WEIGHING_TMPModel GetItem(Expression<Func<RC_WEIGHING_TMP, bool>> whereLambda)
        {
 
                RC_WEIGHING_TMP entity = m_Rep.GetItem(whereLambda);
                if (entity == null) return null;

                RC_WEIGHING_TMPModel model = new RC_WEIGHING_TMPModel();
                              				model.WEIGHING_TMP_ID = entity.WEIGHING_TMP_ID;
				model.PROCESS_ID = entity.PROCESS_ID;
				model.WEIGHING_TYPE = entity.WEIGHING_TYPE;
				model.WEIGHT = entity.WEIGHT;
				model.NODE_ID = entity.NODE_ID;
				model.WEIGHING_USER_ID = entity.WEIGHING_USER_ID;
				model.WEIGHING_TIME = entity.WEIGHING_TIME;
				model.WEIGHING_IMAGE1 = entity.WEIGHING_IMAGE1;
				model.WEIGHING_IMAGE2 = entity.WEIGHING_IMAGE2;
				model.WEIGHING_IMAGE3 = entity.WEIGHING_IMAGE3;
 
                return model; 
        }


		 /// <summary>
        /// 校验Excel数据,这个方法一般用于重写校验逻辑
        /// </summary>
        public virtual bool CheckImportData(string fileName, List<RC_WEIGHING_TMPModel> list,ref ValidationErrors errors )
        {
          
            var targetFile = new FileInfo(fileName);

            if (!targetFile.Exists)
            {

                errors.Add("导入的数据文件不存在");
                return false;
            }

            var excelFile = new ExcelQueryFactory(fileName);

            //对应列头
			 				 excelFile.AddMapping<RC_WEIGHING_TMPModel>(x => x.WEIGHING_TMP_ID, "WEIGHING_TMP_ID");
				 excelFile.AddMapping<RC_WEIGHING_TMPModel>(x => x.PROCESS_ID, "PROCESS_ID");
				 excelFile.AddMapping<RC_WEIGHING_TMPModel>(x => x.WEIGHING_TYPE, "WEIGHING_TYPE");
				 excelFile.AddMapping<RC_WEIGHING_TMPModel>(x => x.WEIGHT, "WEIGHT");
				 excelFile.AddMapping<RC_WEIGHING_TMPModel>(x => x.NODE_ID, "NODE_ID");
				 excelFile.AddMapping<RC_WEIGHING_TMPModel>(x => x.WEIGHING_USER_ID, "WEIGHING_USER_ID");
				 excelFile.AddMapping<RC_WEIGHING_TMPModel>(x => x.WEIGHING_TIME, "WEIGHING_TIME");
				 excelFile.AddMapping<RC_WEIGHING_TMPModel>(x => x.WEIGHING_IMAGE1, "WEIGHING_IMAGE1");
				 excelFile.AddMapping<RC_WEIGHING_TMPModel>(x => x.WEIGHING_IMAGE2, "WEIGHING_IMAGE2");
				 excelFile.AddMapping<RC_WEIGHING_TMPModel>(x => x.WEIGHING_IMAGE3, "WEIGHING_IMAGE3");
 
            //SheetName
            var excelContent = excelFile.Worksheet<RC_WEIGHING_TMPModel>(0);
            int rowIndex = 1;
            //检查数据正确性
            foreach (var row in excelContent)
            {
                var errorMessage = new StringBuilder();
                var entity = new RC_WEIGHING_TMPModel();
						 				  entity.WEIGHING_TMP_ID = row.WEIGHING_TMP_ID;
				  entity.PROCESS_ID = row.PROCESS_ID;
				  entity.WEIGHING_TYPE = row.WEIGHING_TYPE;
				  entity.WEIGHT = row.WEIGHT;
				  entity.NODE_ID = row.NODE_ID;
				  entity.WEIGHING_USER_ID = row.WEIGHING_USER_ID;
				  entity.WEIGHING_TIME = row.WEIGHING_TIME;
				  entity.WEIGHING_IMAGE1 = row.WEIGHING_IMAGE1;
				  entity.WEIGHING_IMAGE2 = row.WEIGHING_IMAGE2;
				  entity.WEIGHING_IMAGE3 = row.WEIGHING_IMAGE3;
 
                //=============================================================================
                if (errorMessage.Length > 0)
                {
                    errors.Add(string.Format(
                        "第 {0} 列发现错误：{1}{2}",
                        rowIndex,
                        errorMessage,
                        "<br/>"));
                }
                list.Add(entity);
                rowIndex += 1;
            }
            if (errors.Count > 0)
            {
                return false;
            }
            return true;
        }

        /// <summary>
        /// 保存数据
        /// </summary>
        public virtual void SaveImportData(IEnumerable<RC_WEIGHING_TMPModel> list)
        {
            try
            {
                using (DBContainer db = new DBContainer())
                {
                    foreach (var model in list)
                    {
                        RC_WEIGHING_TMP entity = new RC_WEIGHING_TMP();
                       						entity.WEIGHING_TMP_ID = model.WEIGHING_TMP_ID;
						entity.PROCESS_ID = model.PROCESS_ID;
						entity.WEIGHING_TYPE = model.WEIGHING_TYPE;
						entity.WEIGHT = model.WEIGHT;
						entity.NODE_ID = model.NODE_ID;
						entity.WEIGHING_USER_ID = model.WEIGHING_USER_ID;
						entity.WEIGHING_TIME = model.WEIGHING_TIME;
						entity.WEIGHING_IMAGE1 = model.WEIGHING_IMAGE1;
						entity.WEIGHING_IMAGE2 = model.WEIGHING_IMAGE2;
						entity.WEIGHING_IMAGE3 = model.WEIGHING_IMAGE3;
 
                        db.RC_WEIGHING_TMP.Add(entity);
                    }
                    db.SaveChanges();
                }
            }
            catch
            {
                throw;
            }
        }


        public virtual bool IsExists(object id)
        {
            return m_Rep.IsExist(id);
        }
		  public void Dispose()
        { 
            
        }

	}
}
